package com.witmore.skyline.web.controller;

import com.witmore.skyline.web.controller.vo.param.DeviceOilCacheWarmUpParamVO;
import com.witmore.skyline.web.controller.vo.param.DeviceSnParam;
import com.witmore.skyline.web.controller.vo.result.ResponseResult;
import com.witmore.skyline.web.service.CameraCaptureTpService;
import com.witmore.skyline.web.service.DeviceService;
import com.witmore.skyline.web.service.RegionDataImportService;
import com.witmore.skyline.web.service.RegionQueryService;
import com.witmore.skyline.web.service.impl.DeviceOilServiceImpl;
import com.witmore.skyline.web.service.third.yjx.dto.DeviceOilDTO;
import com.witmore.skyline.web.utils.LocalDateUtils;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 定时任务手动触发
 * Author: 豆子高
 * Date: 2025/2/28 18:06
 */
@RestController
@RequestMapping("api/develop/")
public class DeveloperController {
    @Autowired
    private DeviceService deviceService;

    @Autowired
    private CameraCaptureTpService cameraCaptureTpService;

    @Autowired
    private RegionDataImportService regionDataImportService;

    @Autowired
    private RegionQueryService regionQueryService;

    @Autowired
    private DeviceOilServiceImpl deviceOilServiceImpl;


    /**
     * 设备列表同步
     */
    @RequestMapping(value = "deviceListSync", name = "设备列表同步")
    public ResponseResult<Void> deviceListSync() {
        deviceService.getAndSaveDeviceListFromCloudm();
        return ResponseResult.success();
    }

    /**
     * 设备详情同步
     */
    @GetMapping(value = "deviceDetailSync", name = "设备详情同步")
    public ResponseResult<Void> deviceDetailSync(DeviceSnParam deviceSnParam) {
        deviceService.deviceFieldMaintenanceFromCloudm(deviceSnParam.getDeviceSn());
        return ResponseResult.success();
    }

    /**
     * 设备日报同步
     */
    @RequestMapping(value = "deviceDailyReportSync", name = "设备日报同步")
    public ResponseResult<Void> deviceDailyReportSync(@RequestParam("startDay") String startDay,
                                                      @RequestParam("endDay") String endDay,
                                                      @RequestParam(value = "skylineDeviceId", required = false) Integer skylineDeviceId) {
        deviceService.deviceDailyFromCloudm(LocalDateUtils.stringConvertDate(startDay),
                LocalDateUtils.stringConvertDate(endDay), skylineDeviceId);
        return ResponseResult.success();
    }

    /**
     * 设备当天工时同步
     */
    @RequestMapping(value = "deviceTodayWorkTimeSync", name = "设备当天工时同步")
    public ResponseResult<Void> deviceTodayWorkTimeSync() {
        deviceService.deviceTodayWorkTimeFromCloudm();
        return ResponseResult.success();
    }

    /**
     * 设备当天工时重置
     */
    @RequestMapping(value = "deviceTodayWorkTimeReset", name = "设备当天工时重置")
    public ResponseResult<Void> deviceTodayWorkTimeReset() {
        deviceService.deviceTodayWorkTimeReset();
        return ResponseResult.success();
    }

    /**
     * 摄像头抓图任务
     */
    @RequestMapping(value = "cameraCaptureTask", name = "摄像头抓图任务")
    public ResponseResult<Void> cameraCaptureTask(@RequestParam(value = "cameraSn", required = false) String cameraSn) {
        cameraCaptureTpService.executeSubmitCapturePicTaskTP(cameraSn);
        return ResponseResult.success();
    }

    /**
     * 摄像头图片修复
     */
    @RequestMapping(value = "cameraPicFix", name = "摄像头图片修复")
    public ResponseResult<Map<String, Integer>> cameraPicFix(@RequestParam(value = "cameraSn", required = false) String cameraSn) {
        return ResponseResult.success(deviceService.cameraPicFix(cameraSn));
    }

    // ========================================
    // 地区数据管理接口
    // ========================================

    /**
     * 导入地区数据
     * 从项目根目录的country_tree.json文件导入全国行政区划数据
     */
    @PostMapping(value = "importRegionData", name = "导入地区数据")
    public ResponseResult<RegionDataImportService.RegionImportResult> importRegionData() {
        RegionDataImportService.RegionImportResult result = regionDataImportService.importRegionDataFromJson();
        return ResponseResult.success(result);
    }

    /**
     * 清空地区数据
     */
    @PostMapping(value = "clearRegionData", name = "清空地区数据")
    public ResponseResult<Integer> clearRegionData() {
        int count = regionDataImportService.clearRegionData();
        return ResponseResult.success(count);
    }

    /**
     * 获取地区数据统计
     */
    @GetMapping(value = "regionStatistics", name = "获取地区数据统计")
    public ResponseResult<RegionDataImportService.RegionStatistics> getRegionStatistics() {
        RegionDataImportService.RegionStatistics statistics = regionDataImportService.getRegionStatistics();
        return ResponseResult.success(statistics);
    }

    /**
     * 填充地区完整名称
     * 根据 acroutes 父级路径拼凑出完整的地区名称
     */
    @PostMapping(value = "fillRegionFullNames", name = "填充地区完整名称")
    public ResponseResult<RegionDataImportService.RegionImportResult> fillRegionFullNames() {
        RegionDataImportService.RegionImportResult result = regionDataImportService.fillFullNames();
        return ResponseResult.success(result);
    }

    // ========================================
    // 地区查询接口
    // ========================================

    /**
     * 根据省市区名称查询对应的adcode
     */
    @GetMapping(value = "getRegionCodeByNames", name = "根据省市区名称查询adcode")
    public ResponseResult<RegionQueryService.RegionCodeInfo> getRegionCodeByNames(
            @RequestParam("province") String province,
            @RequestParam("city") String city,
            @RequestParam(value = "district", required = false) String district) {

        RegionQueryService.RegionCodeInfo result = regionQueryService.getRegionCodeByNames(province, city, district);
        return ResponseResult.success(result);
    }


    /**
     * 手动触发油位缓存预热
     * 用于手动预热指定设备某一天的油位数据到Redis缓存
     */
    @PostMapping(value = "warmUpCache", name = "手动触发油位缓存预热")
    public ResponseResult<List<DeviceOilDTO>> warmUpCache(@Valid @RequestBody DeviceOilCacheWarmUpParamVO paramVO) {
        // 调用Service方法获取指定日期的数据（会自动缓存）
        List<DeviceOilDTO> dayData = deviceOilServiceImpl.getHistoryDayDataFromCache(
                paramVO.getDeviceSn(), paramVO.getDate());
        return ResponseResult.success(dayData);
    }
}
